package com.dld.coupon.activity;

import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Element;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.dld.android.net.Param;
import com.dld.android.persistent.SharePersistent;
import com.dld.android.util.ReflectionFactory;
import com.dld.protocol.image.ImageProtocol;
import com.dld.protocol.json.Protocol;
import com.json.entity.AboutBean;

public class AboutUs extends BaseActivity {

	private ImageView logo;
	private TextView text;
	private TextView footer;

	private Button backButt;
	private static final String CONTENT_INFO_ABOVE = "content_info_above";
	private static final String CONTENT_INFO_BELOW = "content_info_below";
	private static final String TIME_OBTAIN_PREVIOUS = "previous_time";

	private final static int CALLBACK_COMPLETE = 01;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == CALLBACK_COMPLETE) {
				AboutBean mc = (AboutBean) msg.obj;
				bindViewData(mc);
			}
		}
	};

	private void bindViewData(AboutBean n) {
		// 从服务器取图片
		// ImageProtocol imp = new ImageProtocol(this, n.getLogo());
		// imp.startTrans(logo);
		StringBuilder sb = new StringBuilder(1024);
		sb.append(n.getDescription());
		text.setText("      " + sb.toString());
		StringBuilder foot = new StringBuilder(1024);
		foot.append("客服热线:" + n.getKefu() + "\n");
		foot.append("客服邮箱:" + n.getEmail() + "\n");
		foot.append("官方网站：" + n.getSite());
		footer.setText(foot.toString());

	}

	@Override
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.aboutus);
		init();
		initSegment();

	}

	private void init() {
		logo = (ImageView) findViewById(R.id.logo_image);
		text = (TextView) findViewById(R.id.text_description);
		footer = (TextView) findViewById(R.id.text_footer);
		backButt = (Button) findViewById(R.id.back_top_btn);
		backButt.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		// String prev = SharePersistent.get(TIME_OBTAIN_PREVIOUS);
		// if (prev != null) {
		// long timep = Long.parseLong(prev);
		// long ts = System.currentTimeMillis() - timep;
		// if (ts > 1000 * 60 * 60 * 24) {
		// }
		// } else {
		//
		// SharePersistent.set(TIME_OBTAIN_PREVIOUS, ""
		// + System.currentTimeMillis());
		// }
		downLoadNetData();
	}

	private void downLoadNetData() {
		Param param = new Param();
		param.append("r", "www/api/AboutUs").append("userAgent", "dld_android");
		Protocol pro = new Protocol(param);
		pro.startTrans(new Protocol.OnJsonProtocolResult(String.class) {
			@Override
			public void onException(String paramString, Exception paramException) {
				paramException.printStackTrace();
			}

			@Override
			public void onResult(String paramString, Object paramObject) {
				String content = (String) paramObject;
				try {
					Element root = DocumentBuilderFactory
							.newInstance()
							.newDocumentBuilder()
							.parse(new ByteArrayInputStream(content.getBytes()))
							.getDocumentElement();
					AboutBean mc = ReflectionFactory.create(root
							.getElementsByTagName("result").item(0),
							AboutBean.class);
					handler.sendMessage(Message.obtain(handler,
							CALLBACK_COMPLETE, mc));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

}
